package com.ruoyi.web.controller.biz;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BizFang;
import com.ruoyi.system.service.IBizFangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 房源信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/biz/fang")
public class BizFangController extends BaseController
{
    @Autowired
    private IBizFangService fangService;

    /**
     * 获取房源列表
     */
    @PreAuthorize("@ss.hasPermi('biz:fang:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizFang fang)
    {
        startPage();
        List<BizFang> list = fangService.selectFangList(fang);
        return getDataTable(list);
    }
    
    @Log(title = "房源管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('biz:fang:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizFang fang)
    {
        List<BizFang> list = fangService.selectFangList(fang);
        ExcelUtil<BizFang> util = new ExcelUtil<BizFang>(BizFang.class);
        util.exportExcel(response, list, "房源数据");
    }

    /**
     * 根据房源编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:fang:query')")
    @GetMapping(value = "/{fangId}")
    public AjaxResult getInfo(@PathVariable Long fangId)
    {
        return success(fangService.selectFangById(fangId));
    }

    /**
     * 新增房源
     */
    @PreAuthorize("@ss.hasPermi('biz:fang:add')")
    @Log(title = "房源管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BizFang fang)
    {
        if (!fangService.checkFangNameUnique(fang))
        {
            return error("新增房源'" + fang.getFangName() + "'失败，房源名称已存在");
        }
        else if (!fangService.checkFangCodeUnique(fang))
        {
            return error("新增房源'" + fang.getFangName() + "'失败，房源编码已存在");
        }
        fang.setCreateBy(getUsername());
        return toAjax(fangService.insertFang(fang));
    }

    /**
     * 修改房源
     */
    @PreAuthorize("@ss.hasPermi('biz:fang:edit')")
    @Log(title = "房源管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BizFang fang)
    {
        if (!fangService.checkFangNameUnique(fang))
        {
            return error("修改房源'" + fang.getFangName() + "'失败，房源名称已存在");
        }
        else if (!fangService.checkFangCodeUnique(fang))
        {
            return error("修改房源'" + fang.getFangName() + "'失败，房源编码已存在");
        }
        fang.setUpdateBy(getUsername());
        return toAjax(fangService.updateFang(fang));
    }

    /**
     * 删除房源
     */
    @PreAuthorize("@ss.hasPermi('biz:fang:remove')")
    @Log(title = "房源管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fangIds}")
    public AjaxResult remove(@PathVariable Long[] fangIds)
    {
        return toAjax(fangService.deleteFangByIds(fangIds));
    }

    /**
     * 获取房源选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<BizFang> posts = fangService.selectFangAll();
        return success(posts);
    }
}
